package com.aaa.service.impl;

import com.aaa.dao.OperLogDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Dept;
import com.aaa.entity.OperLog;
import com.aaa.service.OperLogService;
import com.aaa.vo.DeptVo;
import com.aaa.vo.OperLogVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Service
public class OperLogServiceImpl extends ServiceImpl<OperLogDao, OperLog> implements OperLogService {
    @Autowired
    private OperLogDao operLogDao;
    @Override
    public void insert(OperLog operLog) {
        operLogDao.insert(operLog);
    }


    //分页查询
    @Override
    public Result<IPage<OperLog>> findByConditions(Integer currentPage, Integer pageSize, OperLogVo operLogVo) {
        IPage<OperLog> page=new Page<>(currentPage,pageSize);
        QueryWrapper<OperLog> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(operLogVo.getOperName())){
            wrapper.like("oper_name",operLogVo.getOperName());
        }
        if(StringUtils.hasText(operLogVo.getMethod())){
            wrapper.like("method",operLogVo.getMethod());
        }
        if(StringUtils.hasText(operLogVo.getOperIp())){
            wrapper.like("oper_ip",operLogVo.getOperId());
        }
        if(StringUtils.hasText(operLogVo.getOperLocation())){
            wrapper.eq("oper_location",operLogVo.getOperLocation());
        }
        if(StringUtils.hasText(operLogVo.getTitle())){
            wrapper.eq("title",operLogVo.getTitle());
        }
        if(Objects.nonNull(operLogVo.getOperId())){
            wrapper.eq("oper_id",operLogVo.getOperId());
        }
        if(Objects.nonNull(operLogVo.getBusinessType())){
            wrapper.eq("business_type",operLogVo.getBusinessType());
        }
        if(Objects.nonNull(operLogVo.getStatus())){
            wrapper.eq("status",operLogVo.getStatus());
        }
        if(Objects.nonNull(operLogVo.getDateRange())&&operLogVo.getDateRange().length==2){
            wrapper.between("create_time",operLogVo.getDateRange()[0],operLogVo.getDateRange()[1]);
        }
        IPage<OperLog> page1 = operLogDao.selectPage(page, wrapper);
        return new Result<IPage<OperLog>>(2000,"查询用户成功",page1);
    }

    //删除
    @Override
    public boolean delById(Integer id) {
        int i = operLogDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }

}
